// Fill out your copyright notice in the Description page of Project Settings.


#include "UI/MainUI.h"

#include "Components/Button.h"
#include "Components/CanvasPanel.h"
#include "Components/TextBlock.h"
#include "Kismet/GameplayStatics.h"
#include "SoulGameplay/SoulKnightPlayerController.h"
#include "UI/MainHUD.h"
#include "UI/OnlineUI.h"

UMainUI::UMainUI(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
	bGameOption = false;
}

void UMainUI::NativeConstruct()
{
	Super::NativeConstruct();
	if (StartUITwinkleCurve)
	{
		//关联曲线
		OnTimelineFloatEvent.BindUFunction(this, FName("OnTwinkleTimelineTick"));
		OnTimelineFinishedEvent.BindUFunction(this, FName("OnTwinkleTimelineEndEvent"));
		StartUITwinkleTimeline.AddInterpFloat(StartUITwinkleCurve, OnTimelineFloatEvent);
		//设置timeline的长度为最后一帧
		StartUITwinkleTimeline.SetTimelineLength(ETimelineLengthMode::TL_LastKeyFrame);
		//设置结束后执行的函数
		StartUITwinkleTimeline.SetTimelineFinishedFunc(OnTimelineFinishedEvent);
		StartUITwinkleTimeline.ReverseFromEnd();
		StartUITwinkleTimeline.SetLooping(true);
		StartUITwinkleTimeline.PlayFromStart();
	}
	//“点击开始”
	ToggleGameOptionButton->OnClicked.AddDynamic(this, &UMainUI::ChangeMenu);
	//“局域网连接”
	OnlineGameButton->OnClicked.AddDynamic(this, &UMainUI::CreateOnlineGame);
	//"新游戏"
	NewGameButton->OnClicked.AddDynamic(this, &UMainUI::NewGame);
	//"退出游戏"
	QuitGameButton->OnClicked.AddDynamic(this, &UMainUI::QuitGame);
}

void UMainUI::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
	Super::NativeTick(MyGeometry, InDeltaTime);
	StartUITwinkleTimeline.TickTimeline(InDeltaTime);
}

void UMainUI::ChangeMenu()
{
	ToggleMenu(!bGameOption);
}

/**
 * @brief 切换菜单
 * @param bShowGameOption true：显示游戏选项。false：隐藏游戏选项
 */
void UMainUI::ToggleMenu(bool bShowGameOption)
{
	if (bShowGameOption)
	{
		BeginStartCanvas->SetVisibility(ESlateVisibility::Collapsed);
		GameOptionCanvas->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
		//播一个切换的动画
		if (SwitchGameOptionAnim)
		{
			PlayAnimation(SwitchGameOptionAnim);
		}
	}
	else
	{
		BeginStartCanvas->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
		// GameOptionCanvas->SetVisibility(ESlateVisibility::Collapsed);

		if (TimerHandle_OnSwitchGameOptionAnimReverseComplete.IsValid())
		{
			GetWorld()->GetTimerManager().ClearTimer(TimerHandle_OnSwitchGameOptionAnimReverseComplete);
		}
		//反向播一个动画
		GetWorld()->GetTimerManager().SetTimer(TimerHandle_OnSwitchGameOptionAnimReverseComplete, this, &UMainUI::OnSwitchGameOptionAnimReverseComplete, 0.5f, false);
		if (SwitchGameOptionAnim)
		{
			PlayAnimationReverse(SwitchGameOptionAnim);
		}
	}
	bGameOption = bShowGameOption;
}

void UMainUI::OnTwinkleTimelineTick(float Output)
{
	BeginStartTip->SetOpacity(Output);
}

void UMainUI::OnTwinkleTimelineEndEvent(float Output)
{
	
}

/**
 * @brief 新游戏
 */
void UMainUI::NewGame()
{
	RemoveFromViewport();
}

/**
 * @brief 退出游戏
 */
void UMainUI::QuitGame()
{
	if (GEngine)
	{
		UKismetSystemLibrary::QuitGame(this, GEngine->GetFirstLocalPlayerController(GetWorld()), EQuitPreference::Quit, false);
	}
}

/**
 * @brief 创建在线局域网游戏
 */
void UMainUI::CreateOnlineGame()
{
	if (ASoulKnightPlayerController* SoulKnightPlayerController = Cast<ASoulKnightPlayerController>(GEngine->GetFirstLocalPlayerController(GetWorld())))
	{
		if (AMainHUD* MainHUD = Cast<AMainHUD>(SoulKnightPlayerController->GetHUD()))
		{
			if (MainHUD->MainUI)
			{
				MainHUD->MainUI->RemoveFromViewport();
			}
			if (MainHUD->OnlineUI)
			{
				MainHUD->OnlineUI->AddToViewport();
				MainHUD->OnlineUI->PlayShowAnimation();
			}
		}
	}
}

void UMainUI::OnSwitchGameOptionAnimReverseComplete()
{
	GameOptionCanvas->SetVisibility(ESlateVisibility::Collapsed);
}
